{template '../web_common/header'}

<!-- 中间内容 -->
<div class="mainContent clearfix">
    <div class="center-con clearfix">
        <h2 class="main-ttl">员工管理</h2>
        <div class="main-ctn clearfix">
            <div class="item  clearfix">
                <span>姓名/昵称：</span>
                <input type="text" class="input-text" name="wx_name" placeholder="请输入姓名/昵称">
                <span class="pl20">状态：</span>
                <select name="status" class="select mr10">
                    <option value="0">全部</option>
                    <option value="1">已授权</option>
                    <option value="2">禁止登录</option>
                </select>
                <button type="button" class="Btn" id="search">筛选</button>
            </div>
            <div class="item  clearfix">
                <button type="button" class="Btn" onclick="window.location.href='{php echo $this->createWebUrl('employee', array('act' => 'fans'));}'">添加</button>
                <button type="button" class="Btn" id="plsq">批量授权</button>
                <button type="button" class="Btn" id="plsc">批量删除</button>
            </div>

            <table data-toggle="table" id="cusTable"></table>

        </div>
    </div>
</div>

<script type="text/javascript">
    require(['jquery', 'bootstrap-table', 'bootstrap-table-zh-CN', 'layer', 'common'], function ($) {

        initTable();

        //跳转分页
        $('body').on('click', '.gotoPage', function () {
            $('#cusTable').bootstrapTable('selectPage', +$('#gotopage-num').val());
        });

        //当点击查询按钮的时候执行
        $("#search").bind('click', function () {
            var wx_name = $('input[name="wx_name"]').val();
            if (wx_name.length === 0) {
                layer.msg('请输入姓名/昵称', {time: 1000});
                return false;
            }
            initTable();
        });
        $('input[name="wx_name"]').on('keydown', function (e) {
            if (e.keyCode === 13) {
                var wx_name = $(this).val();
                if (wx_name.length === 0) {
                    layer.msg('请输入姓名/昵称', {time: 1000});
                    return false;
                }
                initTable();
            }
        })

        //批量授权
        $('#plsq').on('click',function(){
            var lists = $("#cusTable").bootstrapTable('getAllSelections');

            if (lists.length == 0) {
                layer.msg('请勾选');
                return false;
            }

            layer.confirm('确定要授权这些员工?', function () {
                var employee_id = [];
                $.each(lists, function (index,item) {
                    employee_id.push(item.employee_id);
                });

                $.get("{php echo $this->createWebUrl('employee', array('act' => 'auth', 'multi' => '1'))}", {employee_id:employee_id}, function(data){
                    layer.msg(data.msg,{icon: 1,time:1000});
                    if(data.code == 1){
                        setTimeout(function () {
                            initTable();
                        }, 1000);
                    }else{
                        return false;
                    }
                })
            });

        })
        //批量删除
        $('#plsc').on('click',function(){
            var lists = $("#cusTable").bootstrapTable('getAllSelections');

            if (lists.length == 0) {
                layer.msg('请勾选');
                return false;
            }

            layer.confirm('确定要删除这些员工?', function () {
                var employee_id = [];
                $.each(lists, function (index,item) {
                    employee_id.push(item.employee_id);
                });

                $.get("{php echo $this->createWebUrl('employee', array('act' => 'del', 'multi' => '1'))}", {employee_id:employee_id}, function(data){
                    layer.msg(data.msg,{icon: 1,time:1000});
                    if(data.code == 1){
                        setTimeout(function () {
                            initTable();
                        }, 1000);
                    }else{
                        return false;
                    }
                })
            });
        })


    });

    function initTable() {
        //先销毁表格
        $('#cusTable').bootstrapTable('destroy');
        //初始化表格,动态从服务器加载数据
        $("#cusTable").bootstrapTable({
            method: "get",  //使用get请求到服务器获取数据
            url: "{php echo $this->createWebUrl('employee',array('act' => 'index'))}", //获取数据的Servlet地址
            striped: true,  //表格显示条纹
            height: 289,

            pagination: true, //启动分页
            paginationLoop: false,//禁用无限循环的功能
            pageSize: 5,  //每页显示的记录数
            pageNumber: 1, //当前第几页
            pageList: [5, 10, 25, 50],  //记录数可选列表

            clickToSelect: true,
            search: false,  //是否启用查询
            toolbar:"#toolbar",
            showColumns: false,  //显示下拉框勾选要显示的列
            showRefresh: false,  //显示刷新按钮
            sidePagination: "server", //表示服务端请求
            //设置为undefined可以获取pageNumber，pageSize，searchText，sortName，sortOrder
            //设置为limit可以获取limit, offset, search, sort, order

            queryParamsType : "limit",
            queryParams: function queryParams(params) {   //设置查询参数
                var param = {
                    limit: params.limit,
                    offset: params.offset,

                    wx_name:$('input[name=wx_name]').val(),
                    status:$('.item .select option:selected').val()
                };
                return param;
            },
            columns: [
                {field: "选择",title: "选择",align: 'center',valign: 'middle',width:'5%',checkbox: true},
                {field: "employee_id",title: "员工ID",align: 'center',valign: 'middle',width:'7%'},
                {field: "nickname",title: "微信昵称",visible: true,align: 'center',valign: 'middle'},
                {field: "employee_name",title: "姓名",align: 'center',valign: 'middle'},
                {field: "mobile",title: "手机号",align: 'center',valign: 'middle'},
                {field: "remarks",title: "备注",visible: true,align: 'center',valign: 'middle',width:'15%' },
                {field: "status",title: "状态",visible: true,align: 'center',valign: 'middle',formatter:function (value, row, index) {
                    var content = '';
                    if (value == 1) {
                        content += '已授权';
                    } else {
                        content += '禁止登录';
                    }
                    return content;
                }},

                {field: "employee_id",title: '操作',align: 'center',valign: 'middle',width: '15%',formatter:function(value, row, index){
                    var content = row.status == 1 ? '<a class="text-danger" href="javascript:;" onclick="single_auth('+value+', 2)" data-toggle="tooltip" data-placement="bottom" title="取消授权" data-original-title="取消授权" >取消授权</a>' : '<a class="text-primary" href="javascript:;" onclick="single_auth('+value+', 1)" data-toggle="tooltip" data-placement="bottom" title="授权登录" data-original-title="授权登录" >授权登录</a>';
                    content += '&nbsp;&nbsp;' + '<a class="text-primary" href="{php echo $this->createWebUrl('employee', array('act' => 'edit'));}&employee_id='+value+'" data-toggle="tooltip" data-placement="bottom" title="编辑" data-original-title="编辑">编辑</a>' + '&nbsp;&nbsp;' + '<a class="text-primary" href="javascript:;" onclick="del_employee('+value+')"  data-toggle="tooltip" data-placement="bottom" title="删除" data-original-title="删除">删除</a>';
                    return content;
                }}
            ],
            onLoadSuccess: function(){  //加载成功时执行

            },
            onLoadError: function(){  //加载失败时执行
                //
            }
        });
    }

    function del_employee(employee_id) {
        layer.confirm('是否确定删除该员工?', function () {
            if (employee_id == '') {
                layer.msg('参数为空');
                return false;
            }

            $.get("{php echo $this->createWebUrl('employee', array('act' => 'del'))}", {employee_id: employee_id}, function(data){
                layer.msg(data.msg, {icon: 1,time:1000});
                if(data.code == 1){
                    setTimeout(function () {
                        initTable();
                    }, 1000);
                }else{
                    return false;
                }
            })
        });
    }

    //单个授权
    function single_auth(employee_id, status) {
        if (employee_id == '' || status == '') {
            layer.msg('参数为空');
            return false;
        }
        var tip = status == 1 ? '确定要授权该员工?' : '确定要取消该员工的授权?';
        layer.confirm(tip, function () {
            $.get("{php echo $this->createWebUrl('employee', array('act' => 'auth'))}", {employee_id: employee_id, status: status}, function(data){
                layer.msg(data.msg,{icon: 1,time:1000});
                if(data.code == 1){
                    setTimeout(function () {
                        initTable();
                    }, 1000);
                }else{
                    return false;
                }
            })
        });
    }

</script>

{template '../web_common/footer'}